package edu.polsl.bio.str.manager.dao;

import edu.polsl.bio.str.common.entity.DotImage;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * //todo class description
 * <p/>
 * Created on: Jul 27, 2011 4:30:48 PM <br/>
 * <a href="http://www.polsl.pl">www.polsl.pl</a>
 *
 * @author lukasz.olczak@polsl.pl
 */
public class DotImageDaoImpl extends BaseDao implements DotImageDao {
  /**
   * slf4j Logger.
   */
  private static final Logger log = LoggerFactory.getLogger(DotImageDaoImpl.class.getName());

  @Override
  public Long create(DotImage entity) {
    Long id = (Long) getHibernateTemplate().save(entity);
    return id;
  }

  @Override
  public DotImage get(Long refIndividualId, Long queryIndividualId, Long refChromosomeId, Long queryChromosomeId, int width) {
    DetachedCriteria query = DetachedCriteria.forClass(DotImage.class)
        .add(Restrictions.eq("refId", refIndividualId)).add(Restrictions.eq("queryId", queryIndividualId))
        .add(Restrictions.eq("refChromosomeId", refChromosomeId))
        .add(Restrictions.eq("queryChromosomeId", queryChromosomeId)).add(Restrictions.eq("width", width));
    return getUniqueResult(getHibernateTemplate().findByCriteria(query));
  }

  @Override
  @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_UNCOMMITTED)
  public List<DotImage> find(Long refIndividualId, Long queryIndividualId, Long refChromosomeId, Long queryChromosomeId) {
    DetachedCriteria query = DetachedCriteria.forClass(DotImage.class)
        .add(Restrictions.eq("refId", refIndividualId)).add(Restrictions.eq("queryId", queryIndividualId))
        .add(Restrictions.eq("refChromosomeId", refChromosomeId))
        .add(Restrictions.eq("queryChromosomeId", queryChromosomeId));
    return getHibernateTemplate().findByCriteria(query);
  }
}
